package com.jinji.bff.user.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.map.MapUtil;
import com.jinji.bff.user.controller.requestForm.RegisterNewDriverForm;
import com.jinji.common.util.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RestController
@RequestMapping("/driver")
@Tag(name = "DriverController", description = "司机模块Web接口")
public class CustomerController {
    @Resource
    private DriverService driverService;

    @PostMapping("/registerNewDriver")
    @Operation(summary = "新司机注册")
    public R registerNewDriver(@RequestBody @Valid RegisterNewDriverForm form) {
        long driverId = driverService.registerNewDriver(form);
        //在SaToken上面执行登陆，实际上就是缓存userId，然后才有资格拿到令牌
        StpUtil.login(driverId);
        //生成Token令牌字符串（已加密）
        String token = StpUtil.getTokenInfo().getTokenValue();
        return R.ok().put("token", token);
    }

//    @PostMapping("/login")
//    @Operation(summary = "登陆系统")
//    public R login(@RequestBody @Valid LoginForm form) {
//        HashMap map = driverService.login(form);
//        if (map != null) {
//            long driverId = MapUtil.getLong(map, "id");
//            byte realAuth = Byte.parseByte(MapUtil.getStr(map, "realAuth"));
//            boolean archive = MapUtil.getBool(map, "archive");
//            StpUtil.login(driverId);
//            String token = StpUtil.getTokenInfo().getTokenValue();
//
//            return R.ok().put("token", token).put("realAuth", realAuth).put("archive", archive);
//        }
//        return R.ok();
//    }

    @GetMapping("/logout")
    @Operation(summary = "退出系统")
    @SaCheckLogin
    public R logout() {
        StpUtil.logout();
        return R.ok();
    }
}



